<template>
  <div>
    <el-card style="margin: 20px 0px">
      <categourySelect @getcategoryId="getcategoryId" :show="!isShow"></categourySelect>
    </el-card>
    <el-card>
      <div v-show="isShow">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin: 10px 0px"
          @click="addAttr"
          :disabled="!categroy3Id"
          >添加属性</el-button
        >
        <el-table style="width: 100%" border :data="ArrList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150px">
          </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template slot-scope="{ row }">
              <el-tag
                type="success"
                v-for="attrValue in row.attrValueList"
                :key="attrValue.id"
                style="margin: 0px 20px"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="150px">
            <template slot-scope="{ row }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteAttr(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShow">
        <el-form ref="form" :inline="true" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名"
              v-model="attrInfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrInfo.attrName"
          @click="AddValueList"
          >添加属性值</el-button
        >
        <el-button @click="isShow = true">取消</el-button>

        <el-table
          style="width: 100%; margin: 20px 0px"
          border
          :data="attrInfo.attrValueList"
        >
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值"
                size="mini"
                v-if="row.flige"
                @blur="toLook(row)"
                @keyup.native.enter="toLook(row)"
                :ref="$index"
              ></el-input>
              <span
                v-else
                @click="toEdit(row, $index)"
                style="display: block"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row,$index }">
              <el-popconfirm :title="`您确定删除${row.valueName}吗？`" @onConfirm ="deleteAttrList($index)">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="AddOrUpdate" :disabled="attrInfo.attrValueList.length == 0">保存</el-button>
        <el-button @click="isShow = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
//引入深拷贝
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      categroy1Id: "",
      categroy2Id: "",
      categroy3Id: "",
      ArrList: [],
      isShow: true,

      attrInfo: {
        attrName: "", //收集属性名称
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    getcategoryId({ categoryId, level }) {
      if (level == 1) {
        this.categroy1Id = categoryId;
        this.categroy2Id = "";
        this.categroy3Id = "";
      } else if (level == 2) {
        this.categroy2Id = categoryId;
        this.categroy3Id = "";
      } else {
        this.categroy3Id = categoryId;
        this.getArrList();
      }
    },
    async getArrList() {
      const { categroy1Id, categroy2Id, categroy3Id } = this;
      let reslut = await this.$API.attr.reqArrList(
        categroy1Id,
        categroy2Id,
        categroy3Id
      );
      if (reslut.code == 200) {
        this.ArrList = reslut.data;
      }
    },
    AddValueList() {
      //添加属性
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "", //收集属性名
        flige: true,
      });
      //$nextTick 在所有节点加载完成之后执行一次
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    addAttr() {
      (this.isShow = false),
        (this.attrInfo = {
          attrName: "", //收集属性名称
          attrValueList: [],
          categoryId: this.categroy3Id, //收集id
          categoryLevel: 3,
        });
    },
    updateAttr(row) {
      this.isShow = false;
      this.attrInfo = cloneDeep(row);
      this.attrInfo.attrValueList.forEach((item) => {
        //给每一个item添加一个flige的响应式数据
        this.$set(item, "flige", false);
      });
    },
    toLook(row) {
      if (row.valueName.trim() == "") {
        this.$message("请输入正确的属性值");
        return;
      }
      let ispage = this.attrInfo.attrValueList.some((item) => {
        if (item !== row) {
          return item.valueName == row.valueName;
        }
      });
      if (ispage == true) {
        this.$message("请勿输入重复的属性值");
        return;
      }
      row.flige = false;
    },
    toEdit(row, index) {
      row.flige = true;
      //$nextTick 在所有节点加载完毕之后执行一次
      this.$nextTick(() => {
        //focus 自动聚焦
        this.$refs[index].focus();
      });
    },
    deleteAttrList(index) {
      this.attrInfo.attrValueList.splice(index,1)
    },
    async AddOrUpdate() {
      //filter 过滤数组
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item=>{
        if(item.valueName!='') {
          delete item.flige
          return true
        }
      })
     try {
      await this.$API.attr.reqAddAttr(this.attrInfo)
      this.isShow=true
      this.$message({
        type:'success',
        message:'保存成功'
      })
    this.getArrList()
     } catch (error) {
        message:'保存失败'
     }
      
    },
    async deleteAttr(row) {
      let attrId = row.id
        await this.$API.attr.reqDelAttr(attrId)
       try {
         this.$message({
          type:'success',
          message:'删除成功'
        }),
        this.getArrList()
       } catch (error) {
        this.$message('删除失败')
       }
     
    }
  },
};
</script>

<style></style>
